<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
    @include('common-css')
    <style>

        .input_box {
            width: 100%;
            height: 40px;
            margin-top: 15px;
            margin-bottom: 15px;
            display: flex;
            justify-content: flex-start;
            align-items: center;
        }

        .input_label {
            width: 120px;
            box-sizing: border-box;
        }

        .project-table {
            margin-top: 15px;
        }

        .status-tag {
            color: #1890ff;
            border: 1px solid #91d5ff;
            border-radius: 3px;
            background: #e6f7ff;
            font-size: 12px;
            padding: 2px 3px;
        }

        .ant-table-tbody > tr > td, .ant-table-thead > tr > th {
            vertical-align: middle;
            line-height: 25px;
        }

        .search_box {
            width: 1000px;
        }

        .search_box input {
            width: 300px;
        }

        .search_box button {
            margin-left: 20px;
        }

        .search_box .ant-select {
            margin-left: 20px;
        }
    </style>
</head>
<body>

<div id="app"></div>

<script id="appTemplate" type="x-template">
    <a-locale-provider :locale="antdLocale">
        <div class="container">

            @include('committee.header-tpl')

            <main>
                <div class="common_container">

                    @include('committee.aside-tpl')

                    <div class="common_content admin_common_content">

                        <div style="display: flex; position: relative; margin-top: 10px; align-items: center;">
                            <span style="flex: 1;"></span>
                            <a-input style="width: 300px;" placeholder="搜索" v-model="searchKeyword"/>
                            <a-icon v-if="searchKeyword"
                                    style="position: absolute; top: 8px; right: 8px; color: #aaa; cursor: pointer;"
                                    type="cross"
                                    @click="searchKeyword = ''"/>
                        </div>

                        <a-table class="project-table"
                                 rowKey="project_id"
                                 size="small"
                                 :columns="projectTableColumns"
                                 :dataSource="filteredProjects"
                                 :loading="loadingPageData"
                                 :pagination="{pageSize: 10000, hideOnSinglePage: true, position: 'both', showTotal: total => `共 ${total} 项`}">
                            <div slot="application_info"
                                 slot-scope="text, record">
                                项目名称：@{{record.project_name}}<br>
                                申报者：@{{record.applicant}}<br>
                                申报书:<a :href="'/home/view-form?id=' + record.project_id" target="_blank">查看</a><br>
                                附件：<a :href="'/home/view-attachments?project_id=' + record.project_id" target="_blank">查看</a><br>
                            </div>

{{--                            <span slot="application_form" size="large" slot-scope="text, record">--}}
{{--                                <a :href="'/home/view-form?id=' + record.project_id" target="_blank">查看</a>--}}
{{--                            </span>--}}
{{--                            <span slot="attachments" slot-scope="text, record">--}}
{{--                                <a :href="'/home/view-attachments?project_id=' + record.project_id" target="_blank">--}}
{{--                                    <span>查看</span>--}}
{{--                                </a>--}}
{{--                            </span>--}}
                            <span slot="score_time_costs_title">
                                        评分时长
                                        <a-tooltip>
                                            <template slot="title">
                                                从评审专家第一次打开查看申报书页面、查看附件页面或打分页面开始，到其最后一次修改评分时的时长
                                            </template>
                                            <a-icon type="info-circle"/>
                                        </a-tooltip>
                                    </span>
                            <span slot="score_time_costs" size="large" slot-scope="text, record">
                                <template v-for="reviewer_score in record.score_per_reviewer">
                                    <span v-html="formatScoreTimeCost(reviewer_score.score_time_cost)"/><br>
                                </template>
                            </span>
                            <span slot="reviewer_scores" size="large" slot-scope="text, record">
                                <template v-for="reviewer_score in record.score_per_reviewer">
                                    <span>@{{ reviewer_score.score !== null ? reviewer_score.score : '-' }}</span><br>
                                </template>
{{--                                <label v-if="!record.scores_normal"--}}
{{--                                       style="color: red;">@{{ record.scores_desc }}</label>--}}
                            </span>
                            <span slot="is_evidence_material_valid" size="large" slot-scope="text, record">
                                <template v-for="reviewer_score in record.score_per_reviewer">
                                    <label>@{{ reviewer_score.is_evidence_material_valid === null ? '-' :
                                        (reviewer_score.is_evidence_material_valid === 1 ? '是' : '否') }}</label>
                                    <br>
                                </template>
                            </span>
                            <span slot="reviewer_remarks" size="large" slot-scope="text, record">
                                <template v-for="reviewer_score in record.score_per_reviewer">
                                    <a-textarea :disabled="true"
                                                :autosize="{minRows: 1, maxRows: 10}"
                                                v-model="reviewer_score.remark"/>
                                    <br>
                                </template>
                            </span>
                            <span slot="avg_score" size="large" slot-scope="text, record">
                                @{{ record.avg_score !== null ? record.avg_score : '-' }}
                            </span>
                            <span slot="score_status" size="large" slot-scope="text, record">
                                @{{ record.score_status_phrase }}
{{--                                <template v-if="record.score_status == SCORE_STATUS_LEADER_REVIEW_REJECTED">--}}
{{--                                    <br>(@{{ record.large_group_leader_review_remark }})--}}
{{--                                </template>--}}
                            </span>
                            <span slot="status_id" size="large" slot-scope="text, record">
                                 <a-tag v-if="record.status_id == STATUS_MEETING_REVIEW_TO_REVIEW"
                                        color="#2db7f5">是</a-tag>
                                 <a-tag v-else
                                        color="#f50">否</a-tag>

                                <span v-if="record.large_group_leader_review_remark != null">
                                    <p style="font-size: 12px;color: #5e5e5e">@{{ record.status_id == STATUS_MEETING_REVIEW_TO_REVIEW ? '进入会评' : '不进入会评'}}理由</p>
                                    <a-textarea :disabled="true"
                                                :value="record.large_group_leader_review_remark"
                                                :autosize="{minRows: 1, maxRows: 10}"/>
                                </span>
                            </span>
                        </a-table>
                    </div>

                </div>
            </main>

        </div>
    </a-locale-provider>
</script>

@include('common-js')
@include('committee.header-js')
@include('committee.aside-js')

<script>
    var queryParams = parseUrl(location.href).params;
    var awardsInfo = {!! json_encode(array_values(config('vars.awards')), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) !!};

    var projectTableColumns = [
        {
            width: 60,
            title: '序号',
            dataIndex: 'num'
        },
        {
            width: 200,
            title: '申报信息',
            dataIndex:'application_info',
            scopedSlots: {
                customRender: 'application_info'
            }
        },
        // {
        //     width: 150,
        //     title: '项目名称',
        //     dataIndex: 'project_name',
        // },
        // {
        //     width: 100,
        //     title: '申报者',
        //     dataIndex: 'applicant',
        // },
        // {
        //     width: 60,
        //     title: '申报书',
        //     dataIndex: 'application_form',
        //     scopedSlots: {
        //         customRender: 'application_form'
        //     }
        // },
        // {
        //     width: 60,
        //     title: '附件',
        //     dataIndex: 'attachments',
        //     scopedSlots: {
        //         customRender: 'attachments'
        //     }
        // },
        {
            width: 100,
            key: '评分时长',
            slots: {
                title: 'score_time_costs_title'
            },
            dataIndex: 'score_time_costs',
            scopedSlots: {
                customRender: 'score_time_costs'
            }
        },
        {
            width: 70,
            title: '专家评分',
            dataIndex: 'reviewer_scores',
            scopedSlots: {
                customRender: 'reviewer_scores'
            }
        },
        {
            width: 70,
            title: '附件证明材料是否真实有效',
            dataIndex: 'is_evidence_material_valid',
            scopedSlots: {
                customRender: 'is_evidence_material_valid'
            }
        },
        {
            width: 200,
            title: '专家意见',
            dataIndex: 'reviewer_remarks',
            scopedSlots: {
                customRender: 'reviewer_remarks'
            }
        },
        {
            width: 60,
            title: '平均分',
            dataIndex: 'avg_score',
            scopedSlots: {
                customRender: 'avg_score'
            }
        },
        {
            width: 80,
            title: '评分状态',
            dataIndex: 'score_status',
            scopedSlots: {
                customRender: 'score_status'
            }
        },
        {
            width: 80,
            title: '是否进入会评',
            dataIndex: 'status_id',
            scopedSlots: {
                customRender: 'status_id'
            }
        }
    ];
</script>

<script>
    var app = new Vue({
        el: '#app',
        template: '#appTemplate',
        data: {
            profCategoryFilter: '',
            searchKeyword: '',

            addMeetingDialog:false,
            addMeetingRemark:'',
            addMeetingItem:null,

            isAddMeeting:true,
            projectsCount:0,
            setMeetingNumDialog:false,
            setMeetingMode:'number',
            meetingNum:null,
            meetingNumMax:null,
            setNumPlaceholder:'',
            meetingRatio:0,
            meetingCount:0,

            large_group_id:queryParams.large_group_id,


            projectTableColumns,
            pageData: [],
            loadingPageData: false,

            largeGroupId: -1,
            maxAwardRate: 50,

            STATUS_MEETING_REVIEW_TO_REVIEW:'E1',
            STATUS_EXPERT_REVIEW_REJECTED:'D3',
            // 项目的专家组评分状态
            SCORE_STATUS_TO_SCORE: 1,
            SCORE_STATUS_ABNORMAL_SCORES: 2,
            SCORE_STATUS_TO_LEADER_REVIEW: 3,
            SCORE_STATUS_LEADER_REVIEW_PASSED: 4,
            SCORE_STATUS_LEADER_REVIEW_REJECTED: 5,

            LEADER_REVIEW_RESULT_PASSED: '通过',
            LEADER_REVIEW_RESULT_REJECTED: '不通过',

            queryParams,
            awardsInfo,
            profCategories,

            asideSelectedKeys:
                queryParams.aside_key ? [queryParams.aside_key] : [],
            antdLocale:
            antd.locales.zh_CN,
            ...CommitteeHeader.data,
            ...CommitteeAside.data,
            ...Workflow.data,
        },
        computed: {
            atPhaseExpertReview() {
                console.log(this.atPhases(['D'], this.largeGroupId))
                return this.atPhases(['D'], this.largeGroupId)
            },
            notAtPhaseExpertReview() {
                return !this.atPhaseExpertReview
            },
            filteredProjects() {
                var projects = this.searchFilteredProjects;
                if (this.profCategoryFilter) {
                    projects = projects.filter(project => {
                        return project.prof_category == this.profCategoryFilter
                    })
                }
                var num = 1;
                projects.forEach(project => {
                    project.num = num++
                });
                return projects
            },
            searchFilteredProjects() {
                var projects;
                if (this.searchKeyword) {
                    var q = this.searchKeyword.trim();
                    projects = this.pageData.filter(project => {
                        var text = project.project_name + project.applicant_name + project.applicant_org_name + project.applicant_tel + project.applicant_email;
                        return text.toLowerCase().indexOf(q) != -1
                    })
                } else {
                    projects = this.pageData
                }
                return projects
            },
        },
        mounted() {
            this.reloadPageData();
            this.reloadWorkflowPhases()
        },
        watch:{
            meetingNum(val){
                if (val > this.meetingNumMax) {
                    this.meetingNum = val = this.meetingNumMax
                }
                this.changeSetMode(val)
            }
        },
        methods: {
            handleSubmitExpertReviewResult() {
                axios.post('/api/committee/submit-expert-review-result').then(response => {
                    if (response.data.ok) {
                        success('提交成功');
                        this.reloadWorkflowPhases()
                    } else {
                        error('提交失败: ' + response.data.msg)
                    }
                })
            },
            handleLeaderReviewExpertReviewResult(project) {
                axios.post('/api/committee/leader-review-expert-review-result', {
                    projectId: project.project_id,
                    reviewResult: project.large_group_leader_review_result,
                    reviewRemark: project.large_group_leader_review_remark,
                }).then(response => {
                    if (response.data.ok) {
                        success('审核成功');
                        this.reloadPageData()
                    } else {
                        error('审核失败: ' + response.data.msg)
                    }
                })
            },
            isLeaderReviewValid(project) {
                let reviewResult = project.large_group_leader_review_result
                if (!reviewResult) {
                    return false
                }
                let reviewRemark = project.large_group_leader_review_remark
                reviewRemark = reviewRemark ? reviewRemark.trim() : ''
                if (reviewResult == this.LEADER_REVIEW_RESULT_REJECTED && !reviewRemark) {
                    return false
                }
                return true
            },
            formatScoreTimeCost(scoreTimeCost) {
                if (scoreTimeCost === null) {
                    return '-'
                } else if (scoreTimeCost < 60) {
                    return `<span style="color: red;">${scoreTimeCost}秒</span>`
                } else if (scoreTimeCost < 3600) {
                    let minutes = parseInt(scoreTimeCost / 60);
                    let seconds = scoreTimeCost % 60;
                    let content = `${minutes}分钟${seconds}秒`;
                    return scoreTimeCost < 300 ? `<span style="color: red;">${content}</span>` : content
                } else if (scoreTimeCost < 86400) {
                    let hours = parseInt(scoreTimeCost / 3600);
                    let minitues = Math.round(scoreTimeCost % 3600 / 60);
                    return `${hours}小时${minitues}分钟`
                } else {
                    let days = parseInt(scoreTimeCost / 86400);
                    let hours = Math.round(scoreTimeCost % 86400 / 3600);
                    return `${days}天${hours}小时`
                }
            },
            reloadPageData() {
                this.loadingPageData = true;
                if (!(this.large_group_id > 0)) {
                    return  error('加载页面数据失败')
                }
                axios.post('/api/committee/get-manage-large-group-expert-review-data',{large_group_id:this.large_group_id}).then(response => {
                    let data = response.data;
                    if (data.ok) {
                        this.meetingNumMax = this.projectsCount = data.projects.length
                        this.pageData = data.projects;
                        this.largeGroupId = data.large_group.id;
                        //已设置入选项目数时
                        if (typeof data.large_group.attrs.meeting != "undefined") {
                            let project_num = data.large_group.attrs.meeting.project_num
                            this.meetingNum   = project_num
                            this.meetingCount = project_num
                        }
                    } else {
                        error('加载页面数据失败: ' + data.msg)
                    }
                    this.loadingPageData = false
                })
            },


            /**
             * 入选会评弹窗
             */
            addMeeting(item){
                this.meetingDialog(item)
                this.isAddMeeting = true
            },
            /**
             * 移除会评弹窗
             */
            removeMeeting(item){
                this.meetingDialog(item)
                this.isAddMeeting = false
            },

            meetingDialog(item){
                this.addMeetingDialog = true
                this.addMeetingItem = item
                this.addMeetingRemark = item.large_group_leader_review_remark
            },

            /**
             * 入选会评确认
             */
            addMeetingConfirm(){
                if (typeof this.addMeetingRemark != 'string'
                    || this.addMeetingRemark == '' ) {
                    return warn('请填写入选会评理由')
                }
                let param = {
                    project_id:this.addMeetingItem.project_id,
                    remark:this.addMeetingRemark,
                    is_add:this.isAddMeeting
                }
                axios.post('/api/committee/add-or-remove-meeting',param).then(response => {
                    let data = response.data;
                    if (data.ok) {
                        success(data.msg)
                        this.addMeetingItem.status_id = this.isAddMeeting
                            ? this.STATUS_MEETING_REVIEW_TO_REVIEW
                            : this.STATUS_EXPERT_REVIEW_REJECTED
                        this.addMeetingDialog = false
                    } else {
                        error(data.msg)
                    }
                })
            },

            /**
             * 打开设置入选数量弹窗
             */
            openSetMeetingNumDialog(){
                this.setMeetingNumDialog = true
                this.meetingNumMax= this.projectsCount
                this.setNumPlaceholder = '入选数量(0~'+this.meetingNumMax+')'
            },

            /**
             * 设置入选项目模式change（设置比例｜设置数量）
             */
            setNumModelChange(){
                if (this.meetingNum == null){
                    this.meetingNum = 0
                }
                if (this.setMeetingMode == 'number') {
                    this.openSetMeetingNumDialog()
                } else {
                    this.meetingNumMax = 100
                    this.setNumPlaceholder = '入选比例(0~100)'
                }
                this.changeSetMode(this.meetingNum)
            },

            /**
             * 改变模式（比例｜数量时）
             */
            changeSetMode(val){
                if (this.setMeetingMode == 'number') {
                    this.meetingCount = val
                    this.meetingRatio = Math.round((val / this.projectsCount) * 100)
                } else {
                    this.meetingCount = Math.round(this.projectsCount * (val/100))
                    this.meetingRatio = val
                }
            },

            /**
             * 设置入选项目数量
             */
            setMeetingNum(){
                if (this.meetingNum == 0
                    || this.meetingNum == null
                    || this.meetingCount == 0) {
                   return  warn('请设置入选项目数量')
                }

                axios.post('/api/committee/set-meeting-project-num',{meetingNum:this.meetingCount}).then(response => {
                    let data = response.data;
                    if (data.ok) {
                        success(data.msg)
                        this.setMeetingNumDialog = false
                    } else {
                        error(data.msg)
                    }
                })
            },


            ...CommitteeHeader.methods,
            ...CommitteeAside.methods,
            ...Workflow.methods,
        }
    })
</script>

</body>
</html>